/*
 * InclusaoAgencia 
 * 
 * Versao 1.0
 * 
 * 25/05/2013
 * 
 * Autor: Bhetynna Catalunha
 * 
 */
package edu.Ufes.exeSwing.Boundary;

import edu.Ufes.exeSwing.Control.ControlAgencia;
import edu.Ufes.exeSwing.Persistence.Agencias;
import edu.Ufes.exeSwing.Entity.Agencia;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author bhetynnacatalunha
 */
public class InclusaoAgencia extends javax.swing.JFrame {

    /**
     * Creates new form InclusaoAgencia
     */
    TelaListarAgencia janela;
    Agencia agencia;

    public InclusaoAgencia(TelaListarAgencia pJanela, Agencia pAgencia) {
        initComponents();
        this.txtCodigo.grabFocus();
        this.janela = pJanela;
        this.agencia = pAgencia;
        if (pAgencia != null) {
            this.txtCidade.setText(agencia.getCidade());
            this.txtCodigo.setText(agencia.getCodigo());
            this.cmbUFs.setSelectedItem(agencia.getUf());
        }

    }

    private boolean validaCampo() {
        boolean retorno = true;
        if (this.txtCodigo.getText().isEmpty() || this.txtCidade.getText().isEmpty()) {
            retorno = false;
        }
        return retorno;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblCodigo = new javax.swing.JLabel();
        lblCidade = new javax.swing.JLabel();
        lblUF = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        txtCidade = new javax.swing.JTextField();
        cmbUFs = new javax.swing.JComboBox();
        btnSalvar = new javax.swing.JButton();
        btnFechar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Inclusao de Agencia");

        lblCodigo.setText("Codigo:");

        lblCidade.setText("Cidade:");

        lblUF.setText("UF:");

        cmbUFs.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "MT", "MS", "MG", "PR", "PB", "PA", "PE", "PI", "RJ", "RN", "RS", "RO", "RR", "SC", "SE", "SP", "TO" }));

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/Ufes/exeSwing/Imagem/imgSave.gif"))); // NOI18N
        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        btnFechar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/Ufes/exeSwing/Imagem/imgCancel.gif"))); // NOI18N
        btnFechar.setText("Fechar");
        btnFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFecharActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(lblCodigo)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(txtCodigo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 137, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(0, 0, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(lblCidade)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(layout.createSequentialGroup()
                                .add(btnSalvar)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btnFechar))
                            .add(layout.createSequentialGroup()
                                .add(txtCidade, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                                .add(18, 18, 18)
                                .add(lblUF)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(cmbUFs, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(9, 9, 9)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblCodigo)
                    .add(txtCodigo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblCidade)
                    .add(txtCidade, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(cmbUFs, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(lblUF))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 20, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnFechar)
                    .add(btnSalvar)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        // TODO add your handling code here:

        ControlAgencia c = new ControlAgencia();
        if (validaCampo()) {
            if (this.agencia == null) {
                if (c.salvarAgencia(this.txtCodigo.getText(), this.txtCidade.getText(), this.cmbUFs.getSelectedItem().toString())) {
                    JOptionPane.showMessageDialog(null, "Agencia Salva Com Sucesso");
                    this.txtCodigo.setText("");
                    this.txtCidade.setText("");
                    this.janela.carregaAgencias(c.recuperarAgencias());
                }
            } else {
                String codigo = this.agencia.getCodigo();
                this.agencia.setCidade(this.txtCidade.getText());
                this.agencia.setCodigo(this.txtCodigo.getText());
                this.agencia.setUf(this.cmbUFs.getSelectedItem().toString());
                if (c.editarAgencia(this.agencia, codigo)) {
                    JOptionPane.showMessageDialog(null, "Editado");
                    this.dispose();
                    this.janela.carregaAgencias(c.recuperarAgencias());
                    
                }


            }
        } else {
            JOptionPane.showMessageDialog(null, "Preeencha os campos em branco");
        }
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFecharActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnFecharActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFechar;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JComboBox cmbUFs;
    private javax.swing.JLabel lblCidade;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblUF;
    private javax.swing.JTextField txtCidade;
    private javax.swing.JTextField txtCodigo;
    // End of variables declaration//GEN-END:variables
}
